package com.egao.customer.biz.impl;

import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.common.core.damon.constant.UserConstant;
import com.egao.customer.biz.PayHistoryBiz;
import com.egao.customer.entity.PayHistory;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-06-14 21:03:23
 * @version ：1.0
 */
@Repository
public class PayHistoryBizImpl extends BaseDao implements PayHistoryBiz {

	@Override
	public JSONObject listSummaryYear(PageResult2<T> page, PayHistory payHistory) throws Exception {
		Map<String, Object> map  = new HashMap<String, Object>();
		RowMapper<PayHistory> rm = new BeanPropertyRowMapper<PayHistory>(PayHistory.class);
		StringBuffer sql = new StringBuffer("select p.pay_time,sum(p.price) price  ");
		sql.append(" from (select year(pp.pay_time) pay_time,pp.price from sf_pay_history pp where pp.deleted=0  ");
		if (Utils.isNotEmpty(payHistory.getTypes())) {
			sql.append(" and pp.type in (:types) ");
			map.put("types", payHistory.getTypes());
		}
		sql.append(" ) p ");
		sql.append(" GROUP BY p.pay_time ");
		if(Utils.isNotEmpty(payHistory.getYears())){
			sql.append(" having p.pay_time=:years ");
			map.put("years", payHistory.getYears());
		}
		sql.append(" order by p.pay_time desc ");
		return jsonPage2(getPageQueryList3(page, sql.toString(), map, rm));
	}

	@Override
	public JSONObject listSummarySeason(PageResult2<T> page, PayHistory payHistory) throws Exception {
		Map<String, Object> map  = new HashMap<String, Object>();
		RowMapper<PayHistory> rm = new BeanPropertyRowMapper<PayHistory>(PayHistory.class);

		StringBuffer sql = new StringBuffer("select p.pay_time,sum(p.price) price  ");
		sql.append(" from (select CONCAT(year(pp.pay_time),'-第',QUARTER(pp.pay_time),'季度') pay_time,pp.price from sf_pay_history pp where pp.deleted=0 ");
		if (Utils.isNotEmpty(payHistory.getTypes())) {
			sql.append(" and pp.type in (:types) ");
			map.put("types", payHistory.getTypes());
		}
		sql.append(" ) p ");
		sql.append(" GROUP BY p.pay_time  ");
		if(Utils.isNotEmpty(payHistory)){
			if(Utils.isNotEmpty(payHistory.getYears())){
				sql.append(" having instr(p.pay_time,:years)>0 ");
				map.put("years", payHistory.getYears());
			}
		}
		sql.append(" order by p.pay_time desc ");
		return jsonPage2(getPageQueryList3(page, sql.toString(), map, rm));
	}

	@Override
	public JSONObject listSummaryMonth(PageResult2<T> page, PayHistory payHistory) throws Exception {
		Map<String, Object> map  = new HashMap<String, Object>();
		RowMapper<PayHistory> rm = new BeanPropertyRowMapper<PayHistory>(PayHistory.class);
		StringBuffer sql = new StringBuffer("select p.pay_time,sum(p.price) price  ");
		sql.append(" from (select CONCAT(year(pp.pay_time),'-',month(pp.pay_time)) pay_time,pp.price from sf_pay_history pp where pp.deleted=0  ");
		if (Utils.isNotEmpty(payHistory.getTypes())) {
			sql.append(" and pp.type in (:types) ");
			map.put("types", payHistory.getTypes());
		}
		sql.append(" ) p ");
		sql.append(" GROUP BY p.pay_time ");
		if(Utils.isNotEmpty(payHistory)){
			if(Utils.isNotEmpty(payHistory.getYears())){
				sql.append(" having left(p.pay_time,4)=:years ");
				map.put("years", payHistory.getYears());
			}
		}
		sql.append(" order by p.pay_time desc ");
		return jsonPage2(getPageQueryList3(page, sql.toString(), map, rm));
	}

	@Override
	public JSONObject listSummaryWeek(PageResult2<T> page, PayHistory payHistory) throws Exception {
		Map<String, Object> map  = new HashMap<String, Object>();
		RowMapper<PayHistory> rm = new BeanPropertyRowMapper<PayHistory>(PayHistory.class);
//		YEARWEEK(DATE_FORMAT(pay_time,'%Y-%m-%d'),1),DATE_FORMAT(pp.pay_time,'%Y%u')
		StringBuffer sql = new StringBuffer("select p.pay_time,sum(p.price) price  ");
		sql.append(" from (select DATE_FORMAT(pp.pay_time,'%Y年第%u周') pay_time,pp.price from sf_pay_history pp where pp.deleted=0 ");
		if (Utils.isNotEmpty(payHistory.getTypes())) {
			sql.append(" and pp.type in (:types) ");
			map.put("types", payHistory.getTypes());
		}
		if(Utils.isNotEmpty(payHistory)){
			if(Utils.isNotEmpty(payHistory.getMonths())){
				sql.append(" and DATE_FORMAT(pp.pay_time,'%Y-%m')=:months ");
				map.put("months", payHistory.getMonths());
			}
		}
		sql.append(" ) p ");
		sql.append(" GROUP BY p.pay_time ");

		sql.append(" order by p.pay_time desc ");
		return jsonPage2(getPageQueryList3(page, sql.toString(), map, rm));
	}

	@Override
	public JSONObject listSummaryDay(PageResult2<T> page, PayHistory payHistory) throws Exception {
		Map<String, Object> map  = new HashMap<String, Object>();
		RowMapper<PayHistory> rm = new BeanPropertyRowMapper<PayHistory>(PayHistory.class);
		StringBuffer sql = new StringBuffer("select p.pay_time,sum(p.price) price  ");
		sql.append(" from (select DATE_FORMAT(pp.pay_time,'%Y-%m-%d') pay_time,pp.price from sf_pay_history pp where pp.deleted=0 ");
		if (Utils.isNotEmpty(payHistory.getTypes())) {
			sql.append(" and pp.type in (:types) ");
			map.put("types", payHistory.getTypes());
		}
		sql.append(" ) p ");
		sql.append(" GROUP BY p.pay_time ");
		if(Utils.isNotEmpty(payHistory)){
			if(Utils.isNotEmpty(payHistory.getMonths())){
				sql.append(" having left(p.pay_time,7)=:months ");
				map.put("months", payHistory.getMonths());
			}
		}
		sql.append(" order by p.pay_time desc ");
		return jsonPage2(getPageQueryList3(page, sql.toString(), map, rm));
	}

	@Override
	public JSONObject list(PageResult2<T> page, PayHistory payHistory, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<PayHistory> rm = new BeanPropertyRowMapper<PayHistory>(PayHistory.class);
		StringBuffer sql = new StringBuffer("select p.id,p.company_id,p.customer_id");
		sql.append(",p.customer_name,p.type,p.price,p.pic_add,p.pay_time,p.remark");
		sql.append(",p.create_time,p.create_user,p.update_time,p.update_user");
		sql.append(",cmy.name companyName,suser.nick_name createName ");
		sql.append(" from sf_pay_history p ");
		sql.append(" inner join sf_customer cm on cm.id=p.customer_id ");
		sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
		sql.append(" inner join sys_company cmy on cmy.id=p.company_id ");
		sql.append(" where p.deleted=0 and cmy.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and p.company_id=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId() + "");
        }
		if(Utils.isNotEmpty(payHistory)){
			if(Utils.isNotEmpty(payHistory.getCard())){
				sql.append(" and instr(cm.card,:card)>0 ");
				map.put("card", payHistory.getCard());
			}
			if(Utils.isNotEmpty(payHistory.getCustomerId())){
				sql.append(" and instr(p.customer_id,:customer_id)>0 ");
				map.put("customer_id", payHistory.getCustomerId());
			}
			if(Utils.isNotEmpty(payHistory.getCustomerName())){
				sql.append(" and instr(p.customer_name,:customer_name)>0 ");
				map.put("customer_name", payHistory.getCustomerName());
			}
			if(Utils.isNotEmpty(payHistory.getType())){
				sql.append(" and instr(p.type,:type)>0 ");
				map.put("type", payHistory.getType());
			}
			if(Utils.isNotEmpty(payHistory.getSort())&&Utils.isNotEmpty(payHistory.getOrder())){
				sql.append(" order by "+payHistory.getSort()+" "+payHistory.getOrder());
			} else {
				sql.append(" order by p.pay_time desc");
			}
		}
		return jsonPage2(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<PayHistory> list(PayHistory payHistory, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<PayHistory> rm = new BeanPropertyRowMapper<PayHistory>(PayHistory.class);
		StringBuffer sql = new StringBuffer("select p.id,p.company_id,p.customer_id");
		sql.append(",p.customer_name,p.type,p.price,p.pic_add,p.pay_time,p.remark");
		sql.append(",p.create_time,p.create_user,p.update_time,p.update_user");
		sql.append(",cmy.name companyName,suser.username createName ");
		sql.append(" from sf_pay_history p ");
		sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
		sql.append(" inner join sys_company cmy on cmy.id=p.company_id ");
		sql.append(" where p.deleted=0 and cmy.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
			sql.append(" and p.company_id=:tablecompanyId ");
			map.put("tablecompanyId", loginUser.getCompanyId() + "");
		}
		if(Utils.isNotEmpty(payHistory)){
			if(Utils.isNotEmpty(payHistory.getCustomerId())){
				sql.append(" and instr(p.customer_id,:customer_id)>0 ");
				map.put("customer_id", payHistory.getCustomerId());
			}
			if(Utils.isNotEmpty(payHistory.getCustomerName())){
				sql.append(" and instr(p.customer_name,:customer_name)>0 ");
				map.put("customer_name", payHistory.getCustomerName());
			}
			if(Utils.isNotEmpty(payHistory.getType())){
				sql.append(" and instr(p.type,:type)>0 ");
				map.put("type", payHistory.getType());
			}
		}
		sql.append(" order by p.pay_time desc");
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public PayHistory getById(PayHistory payHistory) {
		RowMapper<PayHistory> rm = new BeanPropertyRowMapper<PayHistory>(PayHistory.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select p.id,p.company_id,p.customer_id");
		sql.append(",p.customer_name,p.type,p.price,p.pic_add,p.pay_time,p.remark");
		sql.append(",p.create_time,p.create_user,p.update_time,p.update_user");
		sql.append(",cmy.name companyName,suser.username createName ");
		sql.append(" from sf_pay_history p ");
		sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
		sql.append(" inner join sys_company cmy on cmy.id=p.company_id ");
		sql.append(" where p.deleted=0 and cmy.deleted=0 and p.id=:id ");
		map.put("id", payHistory.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(PayHistory payHistory, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into sf_pay_history (");
		sql.append("company_id,customer_id,customer_name,type,pic_add");
		sql.append(",price,pay_time,remark,create_user,create_time) values ( ");
		sql.append(":company_id,:customer_id,:customer_name,:type,:pic_add");
		sql.append(",:price,now(),:remark,:create_user,now())");
		map.put("company_id", loginUser.getCompanyId() + "");
		map.put("customer_id", payHistory.getCustomerId());
		map.put("customer_name", payHistory.getCustomerName());
		map.put("type", payHistory.getType());
		map.put("pic_add", payHistory.getPicAdd());
		map.put("price", payHistory.getPrice());
		map.put("remark", payHistory.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}


	@Override
	public boolean saveYfk(PayHistory payHistory, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into sf_pay_history (");
		sql.append("company_id,customer_id,customer_name,type,pic_add");
		sql.append(",price,pay_time,remark,create_user,create_time) values ( ");
		sql.append(":company_id,:customer_id,:customer_name,:type,:pic_add");
		sql.append(",:price,:pay_time,:remark,:create_user,now())");
		map.put("company_id", loginUser.getCompanyId() + "");
		map.put("customer_id", payHistory.getCustomerId());
		map.put("customer_name", payHistory.getCustomerName());
		map.put("type", payHistory.getType());
		map.put("pic_add", payHistory.getPicAdd());
		map.put("price", payHistory.getPrice());
		map.put("pay_time", payHistory.getPayTime());
		map.put("remark", payHistory.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean saveJf(PayHistory payHistory, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into sf_pay_history (");
        sql.append("company_id,customer_id,customer_name,type");
		sql.append(",price,pay_time,remark,create_user,create_time) values ( ");
        sql.append(":company_id,:customer_id,:customer_name,:type");
		sql.append(",:price,now(),:remark,:create_user,now())");
        map.put("company_id", loginUser.getCompanyId() + "");
        map.put("customer_id", payHistory.getCustomerId());
        map.put("customer_name", payHistory.getCustomerName());
        map.put("type", payHistory.getType());
        map.put("price", payHistory.getPrice());
        map.put("remark", payHistory.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean saveWg(PayHistory payHistory, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into sf_pay_history (");
        sql.append("company_id,customer_id,customer_name,type");
		sql.append(",pic_add,pay_time,remark,create_user,create_time) values ( ");
        sql.append(":company_id,:customer_id,:customer_name,:type");
		sql.append(",:pic_add,now(),:remark,:create_user,now())");
        map.put("company_id", loginUser.getCompanyId() + "");
        map.put("customer_id", payHistory.getCustomerId());
        map.put("customer_name", payHistory.getCustomerName());
        map.put("type", payHistory.getType());
        map.put("pic_add", payHistory.getPicAdd());
        map.put("remark", payHistory.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(PayHistory payHistory, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sf_pay_history set ");
        sql.append("customer_id=:customer_id,customer_name=:customer_name,type=:type ");
		sql.append(",price=:price ");
		sql.append(",pic_add=:pic_add,pay_time=:pay_time,remark=:remark ");
		sql.append(",update_user=:update_user,update_time=now() where id=:id ");
        map.put("customer_id", payHistory.getCustomerId());
        map.put("customer_name", payHistory.getCustomerName());
        map.put("type", payHistory.getType());
        map.put("price", payHistory.getPrice());
		map.put("pic_add", payHistory.getPicAdd());
        map.put("pay_time", payHistory.getPayTime());
		map.put("remark", payHistory.getRemark());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", payHistory.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean updateWg(PayHistory payHistory, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sf_pay_history set ");
		sql.append(",pic_add=:pic_add,pay_time=:pay_time,remark=:remark ");
		sql.append(",update_user=:update_user,update_time=now() where id=:id ");
        map.put("pic_add", payHistory.getPicAdd());
        map.put("pay_time", payHistory.getPayTime());
        map.put("remark", payHistory.getRemark());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", payHistory.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(PayHistory payHistory, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(payHistory.getId().length()>0){
			for(String ID : payHistory.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update sf_pay_history set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
